package com.android.Bean;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.android.DBTools.ADUS;
import com.android.DBTools.DBConnection;
import com.android.ObjectBean.AnnouncementObjectList;

import net.sf.json.JSONArray;

public class AnnouncementBean {
	public void insertAnnoucement(String annTitle, String annContent, String annType)
			throws ClassNotFoundException, SQLException {
		Date currentdate = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String annDate = simpleDateFormat.format(currentdate);// 时间
		String sql = "INSERT INTO announcement (annID,annTitle,annContent,annType,annDate) VALUES(NULL,\"" + annTitle
				+ "\",\"" + annContent + "\",\"" + annType + "\",\"" + annDate + "\");";
		ADUS.insertData(sql);
		DBConnection.closeCon();
	}

	public String announcemetView() throws ClassNotFoundException, SQLException {
		String sql = "SELECT annTitle, annContent, annType, annDate, annID FROM announcement ";
		List<AnnouncementObjectList> list = new ArrayList<AnnouncementObjectList>();
		ResultSet rs = ADUS.selectData(sql);
		while (rs.next()) {
			AnnouncementObjectList ann = new AnnouncementObjectList();
			ann.setAnnID(rs.getString(rs.findColumn("annID")));
			ann.setAnnTitle(rs.getString(rs.findColumn("annTitle")));
			ann.setAnnContent(rs.getString(rs.findColumn("annContent")));
			ann.setAnnDate(rs.getString(rs.findColumn("annDate")));
			ann.setAnnType(rs.getString(rs.findColumn("annType")));
			list.add(ann);
		}

		DBConnection.closeCon();// 关闭数据库连接
		JSONArray json = JSONArray.fromObject(list);
		return json.toString();
	}
}
